package client;

import com.fasterxml.jackson.annotation.JsonCreator;
import constants.JobConstants;
import kotlin.Metadata;
import kotlin.Triple;
import kotlin.TypeCastException;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
import server.BoardInfoEntry;
import server.MapleUnionFactory;

import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@Metadata(
        mv = {1, 1, 16},
        bv = {1, 0, 3},
        k = 1,
        d1 = {"\u0000@\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\u0010\b\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0015\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0010$\n\u0002\b\u0006\n\u0002\u0010\u0002\n\u0002\b\u0004\u0018\u00002\u00020\u0001B\u0007\b\u0007\u00a2\u0006\u0002\u0010\u0002J\u001a\u0010\u0010\u001a\u0004\u0018\u00010\b2\u0006\u0010\u0011\u001a\u00020\u00052\u0006\u0010\u0012\u001a\u00020\u0005H\u0002J\u0012\u0010\u0013\u001a\u000e\u0012\u0004\u0012\u00020\u0005\u0012\u0004\u0012\u00020\u00060\u0014J\u0012\u0010\u0015\u001a\u000e\u0012\u0004\u0012\u00020\u0005\u0012\u0004\u0012\u00020\u00060\u0014J\u0006\u0010\u0016\u001a\u00020\u000bJ\u0006\u0010\u0017\u001a\u00020\u0005J\u0012\u0010\u0018\u001a\u000e\u0012\u0004\u0012\u00020\u0005\u0012\u0004\u0012\u00020\u00050\u0014J\u0006\u0010\u0019\u001a\u00020\u0005J\b\u0010\u001a\u001a\u00020\u001bH\u0002J\u000e\u0010\u001c\u001a\u00020\u001b2\u0006\u0010\u001d\u001a\u00020\u0005J\u0006\u0010\u001e\u001a\u00020\u001bR\u001a\u0010\u0003\u001a\u000e\u0012\u0004\u0012\u00020\u0005\u0012\u0004\u0012\u00020\u00060\u0004X\u0082\u0004\u00a2\u0006\u0002\n\u0000R\u001a\u0010\u0007\u001a\u000e\u0012\u0004\u0012\u00020\u0005\u0012\u0004\u0012\u00020\b0\u0004X\u0082\u0004\u00a2\u0006\u0002\n\u0000R\u001a\u0010\t\u001a\u000e\u0012\u0004\u0012\u00020\u0005\u0012\u0004\u0012\u00020\u00060\u0004X\u0082\u0004\u00a2\u0006\u0002\n\u0000R\u000e\u0010\n\u001a\u00020\u000bX\u0082\u0004\u00a2\u0006\u0002\n\u0000R\u000e\u0010\f\u001a\u00020\rX\u0082\u0004\u00a2\u0006\u0002\n\u0000R\u000e\u0010\u000e\u001a\u00020\u0005X\u0082\u000e\u00a2\u0006\u0002\n\u0000R\u001a\u0010\u000f\u001a\u000e\u0012\u0004\u0012\u00020\u0005\u0012\u0004\u0012\u00020\u00050\u0004X\u0082\u0004\u00a2\u0006\u0002\n\u0000\u00a8\u0006\u001f"},
        d2 = {"Lclient/MapleUnion;", "", "()V", "allCharacterData", "Ljava/util/HashMap;", "", "Lclient/MapleUnionData;", "boardDatas", "Lclient/BoardData;", "currUnionData", "indexs", "", "lock", "Ljava/util/concurrent/locks/ReentrantReadWriteLock;", "rank", "skills", "contians", "xPos", "yPos", "getAllCharacterMap", "", "getCurrOnionMap", "getIndexs", "getRank", "getSkills", "getTotalLevel", "init", "", "setRank", "n2", "update", "Server"}
)
public final class MapleUnion {
    private final HashMap<Integer, MapleUnionData> allCharacterData = new HashMap();
    private final HashMap<Integer, MapleUnionData> currUnionData = new HashMap();
    private final HashMap<Integer, BoardData> boardDatas = new HashMap();
    private final HashMap<Integer, Integer> skills = new HashMap();
    private final transient ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    private final int[] indexs = new int[16];
    private int rank;

    private final void init() {
        this.lock.writeLock().lock();

        try {
            int i = 0;

            for(int var2 = this.indexs.length; i < var2; ++i) {
                this.indexs[i] = 0;
            }

            this.skills.clear();
            this.boardDatas.clear();
            Map var3 = MapleUnionFactory.INSTANCE.getBoardInfos();
            Iterator var9 = var3.entrySet().iterator();

            while(var9.hasNext()) {
                Entry var8 = (Entry)var9.next();
                int key = ((Number)var8.getKey()).intValue();
                BoardInfoEntry value = (BoardInfoEntry)var8.getValue();
                this.boardDatas.put(key, new BoardData(key, value.getXPos(), value.getYPos(), value.getGroupIndex()));
            }
        } finally {
            this.lock.writeLock().unlock();
        }

    }

    public final void update() {
        this.lock.writeLock().lock();

        try {
            this.init();
            Iterator var2 = this.currUnionData.entrySet().iterator();

            label188:
            while(true) {
                int xPos;
                int yPos;
                int rotate;
                int job;
                int level;
                int grade;
                Map var10000;
                do {
                    MapleUnionData unionData;
                    do {
                        do {
                            BoardInfoEntry boardinfos;
                            do {
                                if (!var2.hasNext()) {
                                    Iterable $receiver$iv = (Iterable)this.boardDatas.values();
                                    Collection destination$iv$iv = (Collection)(new ArrayList());
                                    Iterator var25 = $receiver$iv.iterator();

                                    while(var25.hasNext()) {
                                        Object element$iv$iv = var25.next();
                                        BoardData it = (BoardData)element$iv$iv;
                                        if (it.getExist()) {
                                            destination$iv$iv.add(element$iv$iv);
                                        }
                                    }

                                    $receiver$iv = (Iterable)((List)destination$iv$iv);

                                    int var10001;
                                    int[] var34;
                                    int var35;
                                    for(var2 = $receiver$iv.iterator(); var2.hasNext(); var35 = var34[var10001]) {
                                        Object element$iv = var2.next();
                                        BoardData it = (BoardData)element$iv;
                                        var34 = this.indexs;
                                        var10001 = it.getGroupIndex();
                                        int var10004 = var34[var10001]++;
                                    }
                                    break label188;
                                }

                                Entry entry = (Entry)var2.next();
                                unionData = (MapleUnionData)entry.getValue();
                                boardinfos = (BoardInfoEntry)MapleUnionFactory.INSTANCE.getBoardInfos().get(unionData.getBoardindex());
                            } while(boardinfos == null);

                            xPos = boardinfos.getXPos();
                            yPos = boardinfos.getYPos();
                            rotate = unionData.getRotate();
                            job = unionData.getJob();
                            level = unionData.getLevel();
                            grade = MapleUnionFactory.INSTANCE.getGrade(job, level);
                        } while(grade <= 0);
                    } while(!MapleUnionFactory.INSTANCE.getCardSkillIDs().containsKey(job / 10));

                    var10000 = (Map)MapleUnionFactory.INSTANCE.getCharacterSizeData(JobConstants.getJobBranch(unionData.getJob()), grade - 1);
                } while(var10000 == null);

                Map characterSize = var10000;
                Triple var31 = MapleUnionFactory.INSTANCE.getCardSkillIDs(job, level);
                Integer var32 = var31 != null ? (Integer)var31.getSecond() : null;
                if (var32 == null) {
                    throw new TypeCastException("null cannot be cast to non-null type kotlin.Int");
                }

                int skillid = var32;
                if (this.skills.containsKey(skillid)) {
                    Object var33 = this.skills.get(skillid);
                    if (var33 == null) {
                        Intrinsics.throwNpe();
                    }

                    if (Intrinsics.compare(((Number)var33).intValue(), grade) < 0) {
                        this.skills.put(skillid, grade);
                    }
                } else {
                    this.skills.put(skillid, grade);
                }

                Iterator var14 = characterSize.entrySet().iterator();

                while(var14.hasNext()) {
                    Entry entry2 = (Entry)var14.next();
                    int n = rotate % 1000;
                    int n2 = rotate / 1000;
                    int x = ((Point)entry2.getValue()).x;
                    int y = ((Point)entry2.getValue()).y;
                    switch(n2) {
                        case 1:
                            x *= -1;
                            break;
                        case 2:
                            y *= -1;
                            break;
                        case 3:
                            x *= -1;
                            y *= -1;
                    }

                    BoardData i = this.contians(xPos + (int)Math.round((double)x * Math.cos(Math.toRadians((double)n)) - (double)y * Math.sin(Math.toRadians((double)n))), yPos + (int)Math.round((double)x * Math.sin(Math.toRadians((double)n)) + (double)y * Math.cos(Math.toRadians((double)n))));
                    if (i != null) {
                        i.setExist(true);
                    }
                }
            }
        } finally {
            this.lock.writeLock().unlock();
        }



    }

    public final int getTotalLevel() {
        Iterable $receiver$iv = (Iterable)this.allCharacterData.values();
        int sum$iv = 0;

        int var9;
        for(Iterator var3 = $receiver$iv.iterator(); var3.hasNext(); sum$iv += var9) {
            Object element$iv = var3.next();
            MapleUnionData it = (MapleUnionData)element$iv;
            var9 = it.getLevel();
        }

        return sum$iv;
    }

    public final int getRank() {
        return this.rank;
    }

    public final void setRank(int n2) {
        this.rank = n2;
    }

    private final BoardData contians(int xPos, int yPos) {
        Iterable $receiver$iv = (Iterable)this.boardDatas.values();
        Iterator var4 = $receiver$iv.iterator();

        Object var10000;
        while(true) {
            if (!var4.hasNext()) {
                var10000 = null;
                break;
            }

            Object element$iv = var4.next();
            BoardData it = (BoardData)element$iv;
            if (it.getXPos() == xPos && it.getYPos() == yPos) {
                var10000 = element$iv;
                break;
            }
        }

        return (BoardData)var10000;
    }

    @NotNull
    public final Map<Integer, Integer> getSkills() {
        this.lock.readLock().lock();

        Map var1;
        try {
            var1 = (Map)this.skills;
        } finally {
            this.lock.readLock().unlock();
        }

        return var1;
    }

    @NotNull
    public final Map<Integer, MapleUnionData> getCurrOnionMap() {
        this.lock.readLock().lock();

        Map var1;
        try {
            var1 = (Map)this.currUnionData;
        } finally {
            this.lock.readLock().unlock();
        }

        return var1;
    }

    @NotNull
    public final Map<Integer, MapleUnionData> getAllCharacterMap() {
        this.lock.readLock().lock();

        Map var1;
        try {
            var1 = (Map)this.allCharacterData;
        } finally {
            this.lock.readLock().unlock();
        }

        return var1;
    }

    @NotNull
    public final int[] getIndexs() {
        return this.indexs;
    }

    @JsonCreator
    public MapleUnion() {
        this.init();
    }
}
